package controllers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import models.BackBook;
import models.Borrow;
import models.Indent;
import models.LendCard;
import models.MemType;
import models.Member;

@CRUD.For(BackBook.class)
public class BackBooks extends CRUD {

	//提醒
	public static void remind (){
		String userid = session.get("userid");
		Member meb=Member.findById(Long.parseLong(userid));
		if(meb == null){
			render("/proscenium/memlog.html");
		}
		try{
			MemType memtype = MemType.findById(Long.parseLong(meb.metid));
			LendCard lendcard = LendCard.find("from LendCard where lecnum = ? ", meb.lecnum).first();
			List<Indent> indlist = Indent.find("from Indent where memid = ?",userid).fetch();
			List<Indent> dlist = new ArrayList<Indent>();
			for(Indent idlist:indlist){
				for(Borrow blist:idlist.borrows){
					
					int day = 0 ;
					int dayo = 0 ;
					int tixing = 0 ;
					 Date date_start;
					 Date date_end;
					 Date data_Ztime ;
						SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy-MM-dd HH:mm:ss");   
						Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间   
						String   starttime   =   formatter.format(curDate);
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				        java.util.Date sdtime = format.parse(starttime);
				      
				         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						 date_start = sdf.parse(formatter.format(blist.bortime));
						 date_end = sdf.parse(formatter.format(sdtime));
						 data_Ztime = sdf.parse(formatter.format(blist.borsbt));
						 Calendar cal_start = Calendar.getInstance();
						 Calendar cal_end = Calendar.getInstance();
						 Calendar cal_Ztime = Calendar.getInstance();
						 cal_start.setTime(date_start);
						 cal_end.setTime(date_end);
						 cal_Ztime.setTime(data_Ztime);
						 day = (int) ((date_end.getTime()-date_start.getTime())/(24*60*60*1000));
						 dayo = (int) ((data_Ztime.getTime()-date_start.getTime())/(24*60*60*1000));
						 tixing = dayo - day ;
						 if(tixing == 3){
							 dlist.add(blist.indent);
						 }
					System.out.println("---------------------------ddddddddd----------------------------------||||"+tixing);
				}
			}
			System.out.println("||||||||||||||||||||||||||||"+dlist+"_____________ddd___________________");
			render("/proscenium/remind.html",meb,memtype,lendcard,dlist);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void backall(){
		String userid = session.get("userid");
		Member meb=Member.findById(Long.parseLong(userid));
		if(meb == null){
			render("/proscenium/memlog.html");
		}
		MemType memtype = MemType.findById(Long.parseLong(meb.metid));
		LendCard lendcard = LendCard.find("from LendCard where lecnum = ? ", meb.lecnum).first();
		List<BackBook> blist = BackBook.find("from BackBook where memid = ? ", Long.parseLong(userid)).fetch();
		render("/proscenium/backbookt.html",meb,memtype,lendcard,blist);
	}
	
	public static void forbook(String indnum){
		System.out.println(indnum+"-------------------------------------------");
		Indent indent = Indent.find("from Indent where indnum = ? ", indnum).first();
		render("/BackBooks/forbook.html",indent);
	}
	
	public static void renew (String indnum){
		int state = 0 ;
		try{
			Indent indent= Indent.find("from Indent where indnum = ? ", indnum).first();
			indent.indsta = 6 ;
			indent.save() ;
			state = 1 ;
			render(state);
		}catch(Exception e){
			state = 0 ;
			e.printStackTrace();
			render(state);
		}
	}
	
	public static void ckbackbook(String indnum){
		System.out.println(indnum+"---------------------------------------------------");
		int state = 0 ;
		BackBook backbook = BackBook.find("from BackBook where indnum = ?", indnum).first();
		if(backbook == null){
			state = 1 ;
			render(state);
		}
		render(state);
	}
	
	public static void twbackbook(String indnum){
		String userid = session.get("userid");
		Member meb=Member.findById(Long.parseLong(userid));
		if(meb == null){
			render("/proscenium/memlog.html");
		}
		MemType memtype = MemType.findById(Long.parseLong(meb.metid));
		LendCard lendcard = LendCard.find("from LendCard where lecnum = ? ", meb.lecnum).first();
		Indent indent = Indent.find("from Indent where indnum = ? ",indnum).first();
		System.out.println(indent+"--------------------------------"+indnum);
		List<BackBook> blist = BackBook.find("from BackBook where memid = ? ", Long.parseLong(userid)).fetch();
		render("/proscenium/backbook.html",meb,memtype,lendcard,indent,indnum,blist);
	}
	
	public static void fwbackBook(){
		String userid = session.get("userid");
		Member meb=Member.findById(Long.parseLong(userid));
		if(meb == null){
			render("/proscenium/memlog.html");
		}
		MemType memtype = MemType.findById(Long.parseLong(meb.metid));
		LendCard lendcard = LendCard.find("from LendCard where lecnum = ? ", meb.lecnum).first();
		List<Indent> indentList = Indent.find("from Indent where memid = ?", userid).fetch();
		List<BackBook> backbooklist = BackBook.find("from BackBook where memid = ?",Long.parseLong(userid)).fetch();
		render("/proscenium/backbook.html",meb,memtype,lendcard,indentList,backbooklist);
	}
	
	public static void addbackbookmsg(String memname,String telphone,String mobilephone,String address1,String bookdamagecase,String remark,String indnum){
		int state = 0 ;
		 System.out.println(memname+"1|"+telphone+"2|"+mobilephone+"3|"+address1+"4|"+bookdamagecase+"5|"+remark+"6|"+indnum);
		try{
					String userid =  session.get("userid");
					
					BackBook backbook = new BackBook();
					
					backbook.pName = memname ;
					
					backbook.telphone = telphone ;
					
					backbook.moilephone = mobilephone ;
					
					backbook.addree1 = address1 ;
					
					backbook.bookdamagecase = bookdamagecase ;
					
					backbook.remark = remark ;
					
					backbook.state = 2 ;
					
					backbook.memid = Long.parseLong(userid);
					
					backbook.indnum = indnum ;
					
					SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy-MM-dd HH:mm:ss");   
					Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间   
					String   starttime   =   formatter.format(curDate);
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			        java.util.Date sdtime = format.parse(starttime);
			        
					backbook.ctime = sdtime ;
					
			        backbook.save();
			        
					state = 1 ;
					render(state);
		}catch(Exception e){
			state = 0 ;
			e.printStackTrace();
			render(state);
		}
	}
}
